//
//  MBTheShowViewController.m
//  MiBaoTu
//
//  Created by 彭鹏 on 2016/10/24.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBTheShowViewController.h"
#import "MBSongDetailController.h"
#import "MBSpaceMyParentChildShowTabbarView.h"
#import "MBShowViewController.h"
@interface MBTheShowViewController ()<MBSpaceMyParentChildShowTabbarViewDelegate,UIScrollViewDelegate>
{
    MBSpaceMyParentChildShowTabbarView      *_tabbarView;      //分栏
    NSInteger                               selectTabbarIndex; //分栏选中
    NSMutableArray                          *_ViewCtrlArr;
    UIScrollView                            *_scrollView;
}
@end

@implementation MBTheShowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavBar];
    [self onCreateUI];
    [self addController];
    
    
}

- (void)setupNavBar {
    [self setBackItem];
     [self setTitleView:@"大伽秀"];
    
   [self setRightPlayerItem];
}

#pragma mark - 点击事件
- (void)playingPage {
    
    [self onPlayMusic];
}

- (void)onCreateUI
{
    
    //歌曲、专辑、亲子秀 分栏
    _ViewCtrlArr = [[NSMutableArray alloc]initWithObjects:@"明星主播",@"人气宝宝", nil];
    _tabbarView = [[MBSpaceMyParentChildShowTabbarView alloc]initWithFrame:CGRectMake(0, NAV_HEIGHT, SCREEN_WIDTH, 80*PERCEN) withCount:_ViewCtrlArr withbgColor:[UIColor blackColor] withSelectColor:TINK_BG_COLOR];
    _tabbarView.delegate = self;
    [self.view addSubview:_tabbarView];
    
    //全局滚动
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, _tabbarView.bottom, SCREEN_WIDTH, self.view.height-_tabbarView.bottom)];
    _scrollView.backgroundColor = [UIColor clearColor];
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    _scrollView.bounces = YES;
    [self.view addSubview:_scrollView];
    _scrollView.contentSize = CGSizeMake(_scrollView.width*_ViewCtrlArr.count, _scrollView.height);
    
}

//添加子控件
- (void)addController
{
    for (int i=0; i<_ViewCtrlArr.count; i++) {
        MBShowViewController *show = [[MBShowViewController alloc]init];
        if (i==0) {
            show.type = MBThedakaShowType_starZhubo;
        }
        else if (i==1) {
            show.type = MBThedakaShowType_renqiBaby;
        }
        
        [self addChildViewController:show];
        
        if (i == 0) {
            show.view.frame = CGRectMake(0, 0, _scrollView.width, _scrollView.height);
            [_scrollView addSubview:show.view];
        }
    }
}

#pragma mark - 最新、热门、活动
- (void)onSelectItemForView:(MBSpaceMyParentChildShowTabbarView *)view withIndex:(NSInteger)selectIndex
{
    NSInteger index = _scrollView.contentOffset.x / _scrollView.width;
    if (index != selectIndex) {
        selectTabbarIndex = selectIndex;
        [_scrollView setContentOffset:CGPointMake(selectIndex*_scrollView.width, 0) animated:YES];
    }
}

#pragma mark - 滚动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = _scrollView.contentOffset.x / _scrollView.width;
    if (index != selectTabbarIndex) {
        selectTabbarIndex = index;
        [_tabbarView onUPdateUIForSelectIndex:index];
        
        [self scrollViewDidEndScrollingAnimation:_scrollView];
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    NSInteger index = _scrollView.contentOffset.x / _scrollView.width;
    MBShowViewController *show = self.childViewControllers[index];
    if (show.view.superview) {
        return;
    }
    show.view.frame = scrollView.bounds;
    [_scrollView addSubview:show.view];
}
@end
